package com.buddha.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buddha.api.common.Result;
import com.buddha.api.entity.Model;
import com.buddha.api.service.impl.ModelServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mybatis-plus
 * @since 2023-07-06
 */
@CrossOrigin(origins = {"*"})
@RestController
@RequestMapping("/model")
public class ModelController {
    @Resource
    ModelServiceImpl modelService;

    @PostMapping("/save")
    public Result AddCar(@RequestBody Model model){
        boolean flag;
        if(model.getModelId() == null){
            flag = modelService.save(model);
            return flag?Result.success():Result.error("300","添加失败");
        }else{
            modelService.updateById(model);
            return Result.success();
        }
    }

    @GetMapping("/get")
    public Result GetCar(@RequestParam Integer carId){
        QueryWrapper<Model> wrapper = new QueryWrapper<>();
        wrapper.eq("car_id",carId);
        List<Model> model= modelService.list(wrapper);
        return Result.success(model);
    }

    @PostMapping("/del")
    public Result DelCar(@RequestBody Integer modelId){
        boolean flag = modelService.removeById(modelId);
        return flag?Result.success():Result.error();
    }

    @GetMapping("/id")
    public Result getCarById(@RequestParam Integer carId){
        QueryWrapper<Model> modelQueryWrapper = new QueryWrapper<>();
        modelQueryWrapper.eq("car_id",carId);
        List<Model> modelList = modelService.list(modelQueryWrapper);
        return Result.success(modelList);
    }
}
